﻿using UnityEngine;
using UnityEditor;
using System.IO;
using TMPro;
using UnityEditor.Graphs;
using TMPro.EditorUtilities;
using System.Linq;
using Unity.Plastic.Newtonsoft.Json;
using System.Collections.Generic;
namespace MeEditor
{
        //hierarchy窗口代码
    public class MeHierarchyMenu : MonoBehaviour
    {
        [MenuItem("GameObject/添加参考图", false, 3200)]
        private static void AddMyMenuItem()
        {
            Debug.Log("添加参考图");
            UIEditorHelper.CreateDecorate();
           
        }
        [MenuItem("GameObject/MeEditorTool/全部转小写", false, 3200)]
        private static void ChangeLowerMenuItem()
        {
            Debug.Log("选中的对象子对象全部转小写");
            //如果为选中对象提示选择对象
            if (Selection.activeGameObject == null)
            {
                Debug.Log("请选择对象");
                EditorUtility.DisplayDialog("提示", "请选择对象", "Ok");
                return;
            }
            //读取当权选择的对象的所有子对象 没有激活的对象也需要改动 所以不用GetComponentsInChildren
            
            Transform[] transforms = Selection.activeGameObject.GetComponentsInChildren<Transform>(true);

            foreach (Transform transform in transforms)
            {
                transform.name = transform.name.ToLower();
            }
            Debug.Log("选中的对象子对象全部转小写完成");
            AssetDatabase.SaveAssets();
        }
        [MenuItem("GameObject/MeEditorTool/预设检测", false, 3200)]
        private static void CheckPrefabMenuItem(){
            Debug.Log("检测预设");
            //检查当前选中的Prefab节点下所有子节点有没有miss的资源
            //如果没有miss的资源 提示用户
            PrefabDependencyChecker.ShowWindow();

        }
        //project窗口右键菜单
        [MenuItem("Assets/MeEditorTool/添加spui前缀", false, 3200)]
        private static void addSpuiMenuItem()
        {
            addTypeStr("Texture", "spui_");
        }
         //project窗口右键菜单
        [MenuItem("Assets/MeEditorTool/添加txui前缀", false, 3200)]
        private static void addTxuiMenuItem()
        {
            addTypeStr("Texture", "txui_");
        }
        private static void addTypeStr(string type,string addStr)
        {
            //判断选中是不是文件夹

            if (Selection.activeObject is DefaultAsset)
            {
                //文件夹
                string path = AssetDatabase.GetAssetPath(Selection.activeObject);
                string[] guids = AssetDatabase.FindAssets("t:"+type, new string[] { path });
                foreach (string guid in guids)
                {
                    string assetPath = AssetDatabase.GUIDToAssetPath(guid);
                    string assetName = Path.GetFileNameWithoutExtension(assetPath);
                    assetName = assetName.ToLower();
                    if (!assetName.StartsWith(addStr))
                    {
                        //string newPath = Path.GetDirectoryName(assetPath) + "/" +addStr + assetName + Path.GetExtension(assetPath);
                        AssetDatabase.RenameAsset(assetPath, addStr + assetName);
                    }
                }
            }
            else
            {
                //选择的多个资源全部处理
                // Debug.Log("选中多个资源");
                foreach (UnityEngine.Object obj in Selection.objects)
                {
                    string assetPath = AssetDatabase.GetAssetPath(obj);
                    string assetName = Path.GetFileNameWithoutExtension(assetPath);
                    assetName = assetName.ToLower();
                    //如果包含ui_直接替换
                  
                    // if (assetName.Contains("storehouse"))
                    // {
                    //     assetName = assetName.Replace("storehouse", "bag");
                    // }
                    if (!assetName.StartsWith(addStr))
                    {
                        if (assetName.Contains("ui_"))
                        {
                            assetName = assetName.Replace("ui_", "");
                        }
                        AssetDatabase.RenameAsset(assetPath, addStr + assetName);
                    }
                }
            }
            AssetDatabase.SaveAssets();
            
        }
        [MenuItem("Assets/MeEditorTool/添加pbui前缀", false, 3200)]
        private static void addPbuiMenuItem()
        {
            //判断选中是不是文件夹
            
            if (Selection.activeObject is DefaultAsset)
            {
                //文件夹
                string path = AssetDatabase.GetAssetPath(Selection.activeObject);
                //获取prefab对象
                string[] guids = AssetDatabase.FindAssets("t:Prefab", new string[] { path });
                foreach (string guid in guids)
                {
                    string assetPath = AssetDatabase.GUIDToAssetPath(guid);
                    string assetName = Path.GetFileNameWithoutExtension(assetPath);
                    assetName = assetName.ToLower();
                    //如果包含ui_直接替换
                    Debug.Log("assetName:"+assetName);
                    if (assetName.Contains("troop"))
                    {
                        assetName = assetName.Replace("troop", "unitcard");
                        AssetDatabase.RenameAsset(assetPath, assetName);
                    }
                    if (assetName.Contains("drawcard"))
                    {
                        assetName = assetName.Replace("drawcard", "gacha");
                        AssetDatabase.RenameAsset(assetPath, assetName);
                    }
                    if (!assetName.StartsWith("pbui_"))
                    {
                        AssetDatabase.RenameAsset(assetPath, "pbui_" + assetName);
                    }
                }
            }
            else
            {
                //选择的多个资源全部处理
                // Debug.Log("选中多个资源");
                foreach (UnityEngine.Object obj in Selection.objects)
                {
                    string assetPath = AssetDatabase.GetAssetPath(obj);
                    string assetName = Path.GetFileNameWithoutExtension(assetPath);
                    assetName = assetName.ToLower();
                    if (assetName.Contains("troop"))
                    {
                        assetName = assetName.Replace("troop", "unitcard");
                        AssetDatabase.RenameAsset(assetPath, assetName);
                    }
                    if (assetName.Contains("drawcard"))
                    {
                        assetName = assetName.Replace("drawcard", "gacha");
                        AssetDatabase.RenameAsset(assetPath, assetName);
                    }
                    //兵营
                    if(assetName.Contains("unitcardteam"))
                    {
                        assetName = assetName.Replace("unitcardteam", "barracks");
                        AssetDatabase.RenameAsset(assetPath, assetName);
                    }
                    if (!assetName.StartsWith("pbui_"))
                    {
                        AssetDatabase.RenameAsset(assetPath, "pbui_" + assetName);
                    }
                }
            }
            AssetDatabase.SaveAssets();
            
        }
    }
    [CustomEditor(typeof(Decoreate))]
    public class UIDesignerEditor : Editor
    {
        //添加菜单

        /// <summary>
        /// 在Inspector面板中添加一个按钮，按钮的Name是"加载外部图片"
        /// </summary>
        public override void OnInspectorGUI()
        {
            Decoreate widget = target as Decoreate;
            if (GUILayout.Button("加载外部图片", GUILayout.Height(30)))
            {
                UIEditorHelper.SelectPicForDecorate(widget);
            }
        }
    }
    //Project右键 添加复制全部路径
    public static class CustomMenuItems
    {
         [MenuItem("MeEditor/复制绝对路径 #%X")] // shift ctrl Q
        public static void CopyAssetPath()
        {
            string path = Application.dataPath.Replace("Assets","") + AssetDatabase.GetAssetPath(Selection.activeObject);
            if (!string.IsNullOrEmpty(path))
            {
                GUIUtility.systemCopyBuffer = path;
                Debug.Log("Copied path: " + path);
            }
            else
            {
                Debug.LogWarning("No asset selected or invalid asset.");
            }
        }
    }
    //添加自定義字體
    // [CustomEditor(typeof(TextMeshPro), true), CanEditMultipleObjects]
    public class TextMeshProEditor : TMP_EditorPanelUI
    {
        // 字号颜色数组
        // 读取本地json文件获取字体列表
        private fontStyle[] fontStyles;
        private int selectedFontStyleIndex = -1;
        protected override void OnEnable()
        {
            base.OnEnable();
            LoadFontStyle();
            //初始化字体样式
        }
        private void LoadFontStyle()
        {
            string json = File.ReadAllText(Configure.ResAssetsPath + "/FontStyle.json");
            //读取json转数组
            fontStyles = JsonConvert.DeserializeObject<fontStyle[]>(json);
            //当前字体的颜色是否等于配置的color
            TextMeshPro widget = target as TextMeshPro;
            for (int i = 0; i < fontStyles.Length; i++)
            {   
                //rgba转hex
                

                string color = "#" + ColorUtility.ToHtmlStringRGBA(widget.color);
                //去掉透明度全部转小写
                color = color.Substring(0, 7).ToLower();
                if (color == fontStyles[i].fontColor)
                {
                    selectedFontStyleIndex = i;
                    break;
                }else{
                    selectedFontStyleIndex = 0;
                }
            }
        }
        public override void OnInspectorGUI()
        {
     
            // 添加字体样式的下拉框
            if (fontStyles != null && fontStyles.Length > 0)
            {   
                string[] styleNames = fontStyles.Select(fs => fs.name).ToArray();
                selectedFontStyleIndex = EditorGUILayout.Popup("W3-UI样式", selectedFontStyleIndex, styleNames);
                //设置默认选中的字体样式
                if (selectedFontStyleIndex > 0)
                {
                    TextMeshPro widget = target as TextMeshPro;
                    //字体暂时不选择
                    //strig转color
                    widget.color = ColorUtility.TryParseHtmlString(fontStyles[selectedFontStyleIndex].fontColor, out Color color) ? color : Color.white;
                }
            }
            else
            {
                EditorGUILayout.LabelField("No font styles found.", EditorStyles.boldLabel);
            }
            // 继承原有的面板内容
            base.OnInspectorGUI();
        }

    }

    // 定义字体样式类
    public class fontStyle
    {
        public string name;
        public int fontSize;
        public string fontColor;
    }
}